using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Nb.FactoryAssistant.DAL.Abstracts.Entities;
using Nb.FactoryAssistant.Service.Abstracts;
using Nb.FactoryAssistant.Service.Abstracts.Dto;
using TianShu;
using Volo.Abp.Domain;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Nb.FactoryAssistant.Service
{
    /// <summary>
    /// 仓库类别服务实现类
    /// </summary>
    public class WmsWarehouseTypeServiceImpl : CrudAppService<WmsWarehouseType, WmsWarehouseTypeListOutput, Guid, WmsWarehouseTypeCreateUpdateInput>, IWmsWarehouseTypeService
    {
        public readonly IRepository<WmsWarehouse, Guid> _warehouseRepository;
        public readonly IRepository<WmsWarehouseType, Guid> _warehouseTypeRepository;
        /// <sumary>
        /// 仓库类别构造函数
        /// </sumary>
        public WmsWarehouseTypeServiceImpl(IRepository<WmsWarehouse, Guid> warehouseRepository, IRepository<WmsWarehouseType, Guid> warehouseTypeRepository) : base(warehouseTypeRepository)
        {
            _warehouseRepository = warehouseRepository;
        }



        public override async Task<ResultDto<WmsWarehouseTypeListOutput>> CreateAsync(WmsWarehouseTypeCreateUpdateInput dto)
        {
            var warehouseTypeEntity = await Repository.FindAsync(e => e.Code == dto.Code);
            if (warehouseTypeEntity != null)
            {
                return new ResultDto<WmsWarehouseTypeListOutput>("code不能重复");
            }
            return await base.CreateAsync(dto);
        }
        public override async Task<ResultDto<WmsWarehouseTypeListOutput>> UpdateAsync(Guid id, WmsWarehouseTypeCreateUpdateInput dto)
        {
            var dbUpdateEntity = await Repository.FindAsync(id);
            if (dbUpdateEntity == null)
            {
                return new ResultDto<WmsWarehouseTypeListOutput>($"The '{id}' is not found.");
            }

            if (dto.Code != dbUpdateEntity.Code)
            {
                var dbCodeEntity = await Repository.FindAsync(e => e.Code == dto.Code);
                if (dbCodeEntity != null)
                {
                    return new ResultDto<WmsWarehouseTypeListOutput>($"code不能重复");
                }
            }

            ObjectMapper.Map(dto, dbUpdateEntity);
            dbUpdateEntity = await Repository.UpdateAsync(dbUpdateEntity);
            var data = ObjectMapper.Map<WmsWarehouseType, WmsWarehouseTypeListOutput>(dbUpdateEntity);

            return new ResultDto<WmsWarehouseTypeListOutput>(data);
        }

        public override async Task<ResultDto<bool>> DeleteAsync(IEnumerable<Guid> ids)
        {
            var warehouseEntity =await _warehouseRepository.FindAsync(e => ids.Contains(e.WarehouseTypeId));
            if (warehouseEntity!=null)
            {
                return new ResultDto<bool>($"该类型被仓库【{warehouseEntity.Name}】引用，无法删除");
            }
            return await base.DeleteAsync(ids);
        }
    }
}
